package cn.qvtu.web.controller;

import cn.qvtu.web.domain.Article;
import cn.qvtu.web.service.IArticleService;
import cn.qvtu.web.vo.HotArticleVO;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

@Controller
public class HomeController {
    @Autowired
    private IArticleService articleService;

    @RequestMapping("/")
    public String home(@RequestParam(defaultValue = "1") Integer pageNum, Model model){
        PageHelper.startPage(pageNum, 5);
        List<Article> articleList = articleService.list();
        PageInfo<Article> articlePage = new PageInfo<>(articleList);
        model.addAttribute("articlePage",articlePage);

        PageHelper.startPage(1, 10);
        List<HotArticleVO> hotArticleList = articleService.selectHotArticle();
        model.addAttribute("hotArticleList",hotArticleList);

        return "client/index";
    }





    @RequestMapping("/article/{id}")
    public String article(@PathVariable Long id, Model model){
        Article article = articleService.getById(id);
        if(article == null){
            return "error/404";
        }
        model.addAttribute("article",article);
        return "client/article";
    }


    @GetMapping("/login.html")
    public String login(){
        return "client/login";
    }

}
